package com.changgou.user.service.impl;

import com.changgou.user.config.TokenDecode;
import com.changgou.user.dao.CollectMapper;
import com.changgou.user.pojo.Collect;
import com.changgou.user.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private TokenDecode tokenDecode;


    @Override
    public int save(String id, String username) {

        Collect collect = new Collect();
        collect.setSkuId(id);
        collect.setUsername(username);

        Collect collect1 = new Collect();
        collect1.setUsername(username);
        int count = collectMapper.selectCount(collect1);

        if (count >= 10) {
              return count;
        }

        collectMapper.insertSelective(collect);

        return count;

    }

    @Override
    public List<Collect> findBySkuId(String username) {
        Example example = new Example(Collect.class);
        example.createCriteria().andEqualTo("username",username);
        example.orderBy("id").desc();
        List<Collect> select = collectMapper.selectByExample(example);
        return select;
    }
}
